<?php 
namespace Admin\Controller;

use Think\Controller;

class GoodscategoryController extends AdminController
{
	public function index()
	{
		$category = D('Goodscategory'); // 实例化category对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$list = $category->where('id>0')->order('id')->page($_GET['p'].',10')->select();
		$list = $category->getAdminCate();
		$this->assign('list',$list);// 赋值数据集
		$count = $category->where('id>0')->count();// 查询满足要求的总记录数
		$page = getpage($count,10);;// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->display(); // 输出模板
	}

    public function add()
    {
    	$this->display('Goodscategory/add');
    }
    //添加
	public function doadd()
	{
		$category=D('Goodscategory');
		//初始化数据
		if(!$category->create()){
			$this->error($this->_model->getError());
			exit;
		};
		//执行添加
		if($category->add()>0){
			$this->success("添加成功",U('Goodscategory/index'));
		} else {
			$this->error('添加失败');
		}
	}

	//删除
	public function del()
	{
		$category=D('Goodscategory');
		//拼装删除条件
		$cate['id']=array('eq',I('id'));
		$cate['path']=array('like','%'.I('id').'%');
		$cate['_logic']='or';
		$id=I('id');
		$c=$category->field('id')->where(array('pid'=>$id))->select();
		// var_dump($c);exit;
		if($c){
			$this->error("删除失败,请先删除子分类");
		}else{
			if($category->where($cate)->delete()>0){
				$this->success("删除成功",U('Goodscategory/index'));
			} else {
				$this->error("删除失败");
			}
		}
	}

    public function edit()
    {
        $id=I('get.id/d');
        $data=M('Goodscategory')->find($id); 
        $this->assign('data',$data);
        $this->display();
    }

    public function update()
    {
    	if(empty($_POST)){
    		$this->error('请填写相应数据',U('edit'));
    		exit;
    	}
    	M('Goodscategory')->create();

    	// var_dump(M('Goodscategory')->create());
    	// 	var_dump(M('Goodscategory')->save());exit;
    	if(M('Goodscategory')->save()){
    		$this->success('修改成功',U('index'));
    	}else{
    		// echo M('Goodscategory')->fetchSql(true)->save().'<br>';exit;
    		$this->error('修改失败');
    	}
    }

    public function search()
    {
    	$name=I('post.name');
    	$category=D('Goodscategory');
    	$where['name']=array('like','%'.$name.'%');
    	$list=$category->where($where)->select();
    	$this->assign('list',$list);
    	$this->display('Goodscategory/index');
    }
}